package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManagedService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManagedService managedService;

    /**
     * 分页查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = managedService.findAllUsers(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 查看用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity users(@PathVariable("userID") Long userId) {
        UserInfo userInfo = managedService.users(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 查看视频列表
     *
     * @param page
     * @param pagesize
     * @param uid 用户id
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 Long uid) {
        PageResult pr = managedService.videos(page, pagesize, uid);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看动态列表
     *
     * @param page     页码
     * @param pagesize 条数
     * @param uid      用户id
     * @param state    审核状态
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   Long uid, String state) {
        Integer skr = StringUtils.isEmpty(state) || state.equals("''") ? null : Integer.parseInt(state);
        PageResult pr = managedService.messages(page, pagesize, uid, skr);
        return ResponseEntity.ok(pr);
    }

    /**
     * 用户冻结
     *
     * @param params
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map = managedService.userFreeze(params);
        return ResponseEntity.ok(map);
    }

    /**
     * 解除冻结
     *
     * @param params
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map params) {
        Map map = managedService.userUnFreeze(params);
        return ResponseEntity.ok(map);
    }

    /**
     * 查动态详情
     *
     * @param movementId
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = managedService.findById(movementId);
        return ResponseEntity.ok(vo);
    }
}
